import router, { resetRouter } from '@/router'

const state = {
  routes: [],
  token: ''
}

const mutations = {
  set_token: (state, token) => {
    state.token = token
  }
}

const actions = {
  logout({ commit, state }, args) {
    console.log(args)
    return new Promise((resolve, reject) => {
      localStorage.setItem('token', '')
      commit('set_token', '')
      resetRouter()
      resolve()
    })
  },
  createRoutes({ commit, state }) {
    const constantRoutes = router.options.routes
    console.log(constantRoutes)
    const permissionRoutes = [
      {
        path: '*',
        hidden: true,
        redirect: '/404'
      }
    ]
    state.routes = [...constantRoutes, ...permissionRoutes]
    return state.routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions

}
